#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time    : 2022/7/19 3:49 下午
# @Author  : super_gou
# @File    : fourth.py

# try:
#     f = open('testdata.txt', 'r')
# except:
#     f = open('testdata.txt', 'w')

# num=0
# try:
#     print(num)
# except (NameError, ZeroDivisionError) as result:
#     print(result)

# try:
#     print(1)
# except Exception as result:
#     print(result)
# else:
#     print('我是else，是没有异常的时候执⾏的代码')

# try:
#     f = open('testdata.txt', 'r')
# except Exception as result:
#     f = open('testdata.txt', 'w')
# else:
#     print('没有异常，真开⼼')
# finally:
#     f.close()

# try:
#     f = open('testdata.txt')
#     try:
#         while True:
#             content = f.readline()
#             if len(content) == 0:
#                 break
#             time.sleep(2)
#             print(content)
#     except:
#         print('意外终⽌了读取数据')
#     finally:
#         f.close()
#         print('关闭⽂件')
# except:
#     print("没有这个⽂件")

# ⾃定义异常类，继承Exception
# class ShortInputError(Exception):
#     def __init__(self, length, min_len):
#         self.length = length
#         self.min_len = min_len
#     def __str__(self):
#         return f'你输⼊的⻓度是{self.length}, 不能少于{self.min_len}个字符'
#
# def main():
#     try:
#         con = input('请输⼊密码：')
#         if len(con) < 3:
#             raise ShortInputError(len(con), 3)
#     except Exception as result:
#         print(result)
#     else:
#         print('密码已经输⼊完成')
# main()

'''面向对象'''
# class Washer():
#     def __init__(self):
#         self.width = 500
#         self.height = 800
#     def print_info(self):
#         print(f'洗⾐机的宽度是{self.width}, ⾼度是{self.height}')
# haier1 = Washer()
# haier1.print_info()

# class Washer():
#     def __init__(self, width, height):
#         self.width = width
#         self.height = height
#     def print_info(self):
#         print(f'洗⾐机的宽度是{self.width}')
#         print(f'洗⾐机的⾼度是{self.height}')
# haier1 = Washer(10, 20)
# haier1.print_info()
# haier2 = Washer(30, 40)
# haier2.print_info()

# class Washer():
#     def __init__(self, width, height):
#         self.width = width
#         self.height = height
#     def __str__(self):
#         return '这是海尔洗⾐机的说明书'
# haier1 = Washer(10, 20)
# print(haier1)

# class Washer():
#     def __init__(self, width, height):
#         self.width = width
#         self.height = height
#     def __del__(self):
#         print(f'{self}对象已经被删除')
# haier1 = Washer(10, 20)
# del haier1
'''
一般常用的魔法方法还有
new（优先init调用，静态方法supper().__new）
unicode
call 重载运算符
len长度
repr 类名+object+内存地址
getitem  可迭代
getattr 抛出异常
del 销毁实例化对象 引用数为0时销毁
'''

# class SweetPotato():
#     def __init__(self):
#         # 被烤的时间
#         self.cook_time = 0
#         # 地⽠的状态
#         self.cook_static = '⽣的'
#         # 调料列表
#         self.condiments = []
#     def cook(self, time):
#         """烤地⽠的⽅法"""
#         self.cook_time += time
#         if 0 <= self.cook_time < 3:
#             self.cook_static = '⽣的'
#         elif 3 <= self.cook_time < 5:
#             self.cook_static = '半⽣不熟'
#         elif 5 <= self.cook_time < 8:
#             self.cook_static = '熟了'
#         elif self.cook_time >= 8:
#             self.cook_static = '烤糊了'
#     def __str__(self):
#         return f'这个地⽠烤了{self.cook_time}分钟, 状态是{self.cook_static}, 添加的调料有{self.condiments}'
#     def add_condiments(self, condiment):
#         """添加调料"""
#         self.condiments.append(condiment)
# digua1 = SweetPotato()
# print(digua1)
# digua1.cook(2)
# digua1.add_condiments('酱油')
# print(digua1)
# digua1.cook(2)
# digua1.add_condiments('辣椒⾯⼉')
# print(digua1)
# digua1.cook(2)
# print(digua1)
# digua1.cook(2)
# print(digua1)

# class Furniture():
#     def __init__(self, name, area):
#         self.name = name
#         self.area = area
# class Home():
#     def __init__(self, address, area):
#         self.address = address
#         self.area = area
#         self.free_area = area
#         self.furniture = []
#     def __str__(self):
#         return f'房⼦坐落于{self.address}, 占地⾯积{self.area}, 剩余⾯积{self.free_area}, 家具有{self.furniture}'
#     def add_furniture(self, item):
#         """容纳家具"""
#         if self.free_area >= item.area:
#             self.furniture.append(item.name)
#             self.free_area -= item.area
#         else:
#             print('家具太⼤，剩余⾯积不⾜，⽆法容纳')
# bed = Furniture('双⼈床', 6)
# jia1 = Home('北京', 1200)
# print(jia1)
# jia1.add_furniture(bed)
# print(jia1)
# sofa = Furniture('沙发', 10)
# jia1.add_furniture(sofa)
# print(jia1)
# ball = Furniture('篮球场', 1500)
# jia1.add_furniture(ball)
# print(jia1)

'''继承'''

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
#
# class Prentice(Master):
#     pass
# xiaoming = Prentice()
# print(xiaoming.kongfu)
# xiaoming.make_cake()

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# #注意：当一个类有多个父类的时候，默认使用第一个父类的属性和方法
# class Prentice(School, Master):
#     pass
# xiaoming = Prentice()
# print(xiaoming.kongfu)
# xiaoming.make_cake()

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子煎饼')
#
# class Prentice(School, Master):
#     def __init__(self):
#         self.kongfu = '[徒弟自己独创的酸辣煎饼]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子')
# # 子类和父类具有同名的属性和方法时，默认使用子类自己的属性和方法
# xiaoming = Prentice()
# print(xiaoming.kongfu)
# xiaoming.make_cake()
# print(Prentice.__mro__)

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子煎饼')
# class Prentice(School, Master):
#     def __init__(self):
#         self.kongfu = '[徒弟自己独创的酸辣煎饼]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子')
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
# xiaoming = Prentice()
# xiaoming.make_cake()
# xiaoming.make_master_cake()
# xiaoming.make_school_cake()
# xiaoming.make_cake()

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# class School(Master):
#     def __init__(self):
#         self.kongfu = '[香辣煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子煎饼')
#         super().__init__()
#         super().make_cake()
# class Prentice(School):
#     def __init__(self):
#         self.kongfu = '[徒弟自己独创的酸辣煎饼]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子')
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
#     def make_old_cake(self):
#         super().__init__()
#         super().make_cake()
# xiaoming = Prentice()
# xiaoming.make_cake()
# xiaoming.make_master_cake()
# xiaoming.make_school_cake()
# xiaoming.make_cake()

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子煎饼')
# class Prentice(School, Master):
#     def __init__(self):
#         self.kongfu = '[徒弟自己独创的酸辣煎饼]'
#         self.__money = 2000000
#     def __info_print(self):
#         print(self.kongfu)
#         print(self.__money)
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子')
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
#
# class Tusun(Prentice):
#     pass
# xiaoming = Prentice()
# print(xiaoming.__money)
# xiaoming.__info_print()
#
# xiaogang = Tusun()
# print(xiaogang.__money)

# class Master(object):
#     def __init__(self):
#         self.kongfu = '[五香煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的煎饼')
# class School(object):
#     def __init__(self):
#         self.kongfu = '[香辣煎饼果子配方]'
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子煎饼')
# class Prentice(School, Master):
#     def __init__(self):
#         self.kongfu = '[徒弟自己独创的酸辣煎饼]'
#         self.__money = 2000000
#     def get_money(self):
#         return self.__money
#     def set_money(self):
#         self.__money = 500
#     def __info_print(self):
#         print(self.kongfu)
#         print(self.__money)
#     def make_cake(self):
#         print(f'运用{self.kongfu}制作的果子')
#     def make_master_cake(self):
#         Master.__init__(self)
#         Master.make_cake(self)
#     def make_school_cake(self):
#         School.__init__(self)
#         School.make_cake(self)
#
# class Tusun(Prentice):
#     pass
# xiaoming = Prentice()
# xiaogang = Tusun()
# #  get_money money
# print(xiaogang.get_money())
# #  set_money money
# xiaogang.set_money()
# print(xiaogang.get_money())
'''面向对象-其他'''
# class Dog(object):
#     def work(self): # ⽗类提供统⼀的⽅法，哪怕是空⽅法
#         print('指哪打哪...')
# class ArmyDog(Dog): # 继承Dog类
#     def work(self): # ⼦类重写⽗类同名⽅法
#         print('追击敌⼈...')
# class DrugDog(Dog):
#     def work(self):
#         print('追查毒品...')
# class Person(object):
#     # 传⼊不同的对象，执⾏不同的代码，即不同的work函数
#     def work_with_dog(self, dog):
#         dog.work()
# ad = ArmyDog()
# dd = DrugDog()
# daqiu = Person()
# daqiu.work_with_dog(ad)
# daqiu.work_with_dog(dd)

# class Dog(object):
#     tooth = 10
# wangcai = Dog()
# xiaohei = Dog()
# print(Dog.tooth)
# print(wangcai.tooth)
# print(xiaohei.tooth)

# class Dog(object):
#     tooth = 10
# wangcai = Dog()
# xiaohei = Dog()
# # 修改类属性
# Dog.tooth = 12
# print(Dog.tooth)
# print(wangcai.tooth)
# print(xiaohei.tooth)
# # 不能通过对象修改属性，如果这样操作，实则是创建了⼀个实例属性
# wangcai.tooth = 20
# print(Dog.tooth)
# print(wangcai.tooth)
# print(xiaohei.tooth)

# class Dog(object):
#     def __init__(self):
#         self.age = 5
#     def info_print(self):
#         print(self.age)
# wangcai = Dog()
# print(wangcai.age) # 5
# # print(Dog.age) # 报错：实例属性不能通过类访问
# wangcai.info_print()

# class Dog(object):
#     __tooth = 10
#     @classmethod
#     def get_tooth(cls):
#         return cls.__tooth
# wangcai = Dog()
# result = wangcai.get_tooth()
# print(result)

# class Dog(object):
#     @staticmethod
#     def info_print():
#         print('这是⼀个狗类，⽤于创建狗实例....')
# wangcai = Dog()
# # 静态⽅法既可以使⽤对象访问⼜可以使⽤类访问
# wangcai.info_print()
# Dog.info_print()

# import math
# print(math.sqrt(9))
#
# import time as tt
# tt.sleep(2)
# print('hello')
# # 功能别名
# from time import sleep as sl
# sl(2)
# print('hello')